package xiaohao.order.manager.analyze.impl;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.google.common.collect.Lists;
import com.taobao.api.domain.Trade;

import xiaohao.order.constants.TradeStatus;
import xiaohao.order.dao.ItemDao;
import xiaohao.order.dao.UserDao;
import xiaohao.order.domain.analyze.TradeCycleDO;
import xiaohao.order.manager.analyze.TradeCycleAnalyzeManager;

public class DefaultTradeCycleAnalyzeManager implements TradeCycleAnalyzeManager {

	private final Logger logger = Logger.getLogger(getClass());
	
	@Override
	public List<TradeCycleDO> getTradeCycles(List<Trade> trades) {
		List<TradeCycleDO> tradeCycleDOs = Lists.newArrayList();
		for (Trade trade : trades) {
			Date created = trade.getCreated();
			Date payTime = trade.getPayTime();
			Date consignTime = trade.getConsignTime();
			Date endTime = trade.getEndTime();
			
			if (TradeStatus.TRADE_FINISHED.name().equals(trade.getStatus())) {
				TradeCycleDO tradeCycleDO = new TradeCycleDO();
				tradeCycleDO.setCreate_pay(getIntervalDays(created, payTime));
				tradeCycleDO.setPay_delivery(getIntervalDays(payTime, consignTime));
				tradeCycleDO.setDelivery_confirm(getIntervalDays(consignTime, endTime));
				tradeCycleDOs.add(tradeCycleDO);
			}
		}
		return tradeCycleDOs;
	}
	
	private float getIntervalDays(Date startday, Date endday) {
		if (startday.after(endday)) {
			throw new IllegalArgumentException();
		}
		
		long sl = startday.getTime();
		long el = endday.getTime();
		float ei = (float) (el - sl);
		return ei / (1000 * 60 * 60);
	}
	
	private ItemDao itemDao;
	
	private UserDao userDao;

	public void setItemDao(ItemDao itemDao) {
		this.itemDao = itemDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

}
